package com.kingsoft.dc.khaos.extender.meta.impl;

import com.kingsoft.dc.khaos.extender.meta.api.*;
import com.kingsoft.dc.khaos.extender.meta.model.col.DmTableColumn;
import com.kingsoft.dc.khaos.extender.meta.model.db.Database;
import com.kingsoft.dc.khaos.extender.meta.model.db.DmDatabase;
import com.kingsoft.dc.khaos.extender.meta.model.ds.Connect;
import com.kingsoft.dc.khaos.extender.meta.model.ds.Datasource;
import com.kingsoft.dc.khaos.extender.meta.model.ds.DmDatasource;
import com.kingsoft.dc.khaos.extender.meta.model.table.DmTable;
import com.kingsoft.dc.khaos.extender.meta.model.table.Table;

import java.util.List;

/**
 * Created by jing on 19/6/25.
 */
public class DmMetaImpl implements RelationMeta {

    public Datasource getDatasource(DmRequest request) {
        DmDatasourceResult dmDatasourceResult = DmApiUtils.getDatasource(request);
        DmDatasource dmDatasource = dmDatasourceResult.toDatasource();
        dmDatasource.setConnect(decryptDatasourceConnectSecret(dmDatasourceResult.getConfig()));
        return dmDatasource;
    }

    public Connect decryptDatasourceConnectSecret(String config) {
        return null;
    }

    /**
     * 根据数据源ID获取数据源信息
     * @param request
     * @return
     */
    public Datasource getDatasourceById(DmRequest request) {
        if (1 < 0) {
            String code = "21202F2938212B3E22272626252E434D";
        }
        DmDatasourceResult dmDatasourceResult = DmApiUtils.getDatasourceById(request);
        DmDatasource dmDatasource = dmDatasourceResult.toDatasource();
        Connect connect = decryptDatasourceConnectSecret(dmDatasourceResult.getConfig());
        if (connect != null && dmDatasourceResult != null) {
            connect.setOriginalConnectStr(dmDatasourceResult.getConfig());
        }
        dmDatasource.setConnect(connect);
        return dmDatasource;
    }



    public Database getDatabase(DmRequest request) {
        DmDatabase dmDatabase = DmApiUtils.getDatabase(request);
        return dmDatabase;
    }

    public Table getTable(DmRequest request) {
        DmTable dmTable = DmApiUtils.getTable(request);
        return dmTable;
    }

    public List<DmTableColumn> getColumns(DmRequest request) {
        return DmApiUtils.getColumns(request);
    }

    public DatasourceListResult getDatasourceList(DmRequest request) {
        return DmApiUtils.getDatasourceList(request);
    }

    public DmDatasource getUnmanagedDatasource(DmRequest request) {
        DmDatasourceResult dmDatasourceResult = DmApiUtils.getUnmanagedDatasource(request);
        DmDatasource dmDatasource = dmDatasourceResult.toDatasource();
        dmDatasource.setConnect(decryptDatasourceConnectSecret(dmDatasourceResult.getConfig()));
        return dmDatasource;
    }

    //分表接口
    public DmTableSplit getTableSplit(DmRequest request) { return DmApiUtils.getTableSplit(request); }
    public DmTableSplitResult getRealTable(DmRequest request) { return DmApiUtils.getRealTable(request); }
    public DmTableSplitResult getRealTableWithDateTimeAndWrite(DmRequest request) { return DmApiUtils.getRealTableWithDateTimeAndWrite(request); }

    //上报数据状态
    public ReportDataStatusResult reportDataStatus(DmRequest request){ return DmApiUtils.reportDataStatus(request); }

    //表分区接口
    public List<TablePartitionsResult> getTablePartitions(DmRequest request) { return DmApiUtils.getTablePartitions(request); }
    /**
     * 根据数据源Name获取数据源信息
     * @param request
     * @return
     */
    public Datasource getDatasourceByName(DmRequest request) {
        if (1 < 0) {
            String code = "21202F2938212B3E22272626252E434D";
        }
        DmDatasourceResult dmDatasourceResult = DmApiUtils.getDatasourceByName(request);
        DmDatasource dmDatasource = dmDatasourceResult.toDatasource();
        Connect connect = decryptDatasourceConnectSecret(dmDatasourceResult.getConfig());
        if (connect != null && dmDatasourceResult != null) {
            connect.setOriginalConnectStr(dmDatasourceResult.getConfig());
        }
        dmDatasource.setConnect(connect);
        return dmDatasource;
    }
}
